package batch.monitor.client;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import org.apache.commons.lang.time.DateFormatUtils;

import batch.monitor.server.BatchMngMBean;

public class JMXClientListener implements NotificationListener {
	private JMXConnector jmxc = null;
	private String targetObejctName = "";
	private JPanel batchJobPanel = null;
	private JTextArea textArea = null;
	private StringBuffer detailResult = null;
	JMXClientWindow window = null;
	
	public JMXClientListener(JMXConnector jmxConnector, String targetObjectName, JMXClientWindow window) {
		this.jmxc = jmxConnector;
		this.targetObejctName = targetObjectName;
		this.batchJobPanel = window.addBatchJob();
		this.window = window;
		
		detailResult = new StringBuffer();
		textArea = new JTextArea();
		textArea.setEditable(false);
	}
	
	public void handleNotification(Notification notification, Object handback) {
		try {
			
			MBeanServerConnection msbc = jmxc.getMBeanServerConnection();
			ObjectName name = new ObjectName(targetObejctName);
			
			System.out.println("\n\nReceived notification:");
			System.out.println("ClassName: " + notification.getClass().getName());
			System.out.println("Source: " + notification.getSource());
			System.out.println("Type: " + notification.getType());
			System.out.println("Message: " + notification.getMessage());
			System.out.println("=========================================================");
			this.catchMbean(msbc, name);
			System.out.println("=========================================================");
					
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void catchMbean(MBeanServerConnection msbc, ObjectName name) {
		BatchMngMBean batchMngMBean = JMX.newMBeanProxy(msbc, name, BatchMngMBean.class);
		String date = DateFormatUtils.format(new Date(), "hh:mm:ss");
		detailResult.append("start time : " + date + "\n");
		detailResult.append("\n");
		detailResult.append("BatchCfgName : " + batchMngMBean.getBatchCfgName() + "\n");
		detailResult.append("BatchDescription : " + batchMngMBean.getBatchDescription() + "\n");
		detailResult.append("BatchFullCount : " + batchMngMBean.getBatchFullCount() + "\n");
		detailResult.append("BatchCurrentLoopCount : " + batchMngMBean.getBatchCurrentLoopCount() + "\n");
		detailResult.append("\n");
		detailResult.append("MaxMemory :  " + batchMngMBean.getMaxMemory() + "\n");
		detailResult.append("TotalMemory : " + batchMngMBean.getTotalMemory() + "\n");
		detailResult.append("FreeMemory :  " + batchMngMBean.getFreeMemory() + "\n");
		detailResult.append("\n");
		detailResult.append("Company : " + batchMngMBean.getCompany() + "\n");
		detailResult.append("Item : " + batchMngMBean.getItem() + "\n");
		detailResult.append("Person : " + batchMngMBean.getPerson() + "\n");
		detailResult.append("Quantity : " + batchMngMBean.getQuantity() + "\n");
		detailResult.append("\n");
		detailResult.append("\n");
		System.out.println(detailResult);
		
		JProgressBar progressBar = (JProgressBar)batchJobPanel.getComponent(0);
		JButton button = (JButton)batchJobPanel.getComponent(1);
		
		batchJobPanel.setBorder(BorderFactory.createTitledBorder(batchMngMBean.getBatchCfgName()));
		
		progressBar.setToolTipText("Description : " + batchMngMBean.getBatchDescription()
				               + ", Progress : " + batchMngMBean.getBatchCurrentLoopCount() + " / " + batchMngMBean.getBatchFullCount()
				               + ", MaxMemory : " + batchMngMBean.getMaxMemory()
				               + ", TotalMemory : " + batchMngMBean.getTotalMemory()
				               + ", FreeMemory : " + batchMngMBean.getFreeMemory());
		int progress = (batchMngMBean.getQuantity().intValueExact() * 100) / batchMngMBean.getBatchFullCount();
		progressBar.setValue(progress);
		
		long memory = (batchMngMBean.getFreeMemory() * 100) / batchMngMBean.getTotalMemory();
		if(memory <= 30){
			progressBar.setForeground(Color.RED);
		}

		if(progress == 100){
			progressBar.setForeground(Color.GRAY);
			detailResult.append("========================= Batch complete!!! =========================");
			detailResult.append("\n");
			
		}
		
		textArea.setText(detailResult.toString());
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JSplitPane splitPane = (JSplitPane)window.getContentPane().getComponent(0);
				JScrollPane rightPane = (JScrollPane)splitPane.getRightComponent();
				
				rightPane = new JScrollPane(textArea);
				rightPane.setBorder(BorderFactory.createTitledBorder("Detail"));
				splitPane.setDividerLocation(window.getWidth()/2);
				splitPane.setRightComponent(rightPane);
			}
		});
	}

}